package com.rpp.controllerr;

import com.rpp.domain.Order;
import com.rpp.service.IOrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author peng
 * @Date 2020/10/28 10:55 上午
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @Autowired
    AmqpTemplate rabbitTemplate;

    /**
     * 查询订单列表
     *
     * @return
     */
    @GetMapping("/findAll")
    public List<Order> findAll() {
        List<Order> list = orderService.findAll();
        return list;
    }

    /**
     * 创建订单
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public Order createOrder() throws Exception {
        Order order = orderService.createOrder();

        //创建成功后，发送订单消息，过期自动进入死信队列
//        rabbitTemplate.convertAndSend("ex.order.ttl-waiting", "order.ttl-waiting", order.getId());

        MessageProperties properties = new MessageProperties();
//        properties.setExpiration("1 * 60 * 1000");
        Message message = new Message(order.getId().toString().getBytes("utf-8"), properties);
        rabbitTemplate.convertAndSend("ex.order.ttl-waiting", "order.ttl-waiting", message);

        return order;
    }

    /**
     * 支付订单，支付成功后更新订单状态为已支付，消费之前队列中的消息
     * 过期未支付进入死信队列
     *
     * @param id
     */
    @PostMapping("/pay")
    public Map pay(Long id) {
        return orderService.payOrder(id);
    }
}
